<?php
class Kbs_Application_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
    protected $_view;

    /**
     * To init the view
     *
     * @return Zend_View $view
     */
    public function init()
    {
        if (null === $this->_view) {
            $options = $this->getOptions();
            $view = new Zend_View($options);

            if (!empty($options['params'])) {
                foreach ($options['params'] as $key => $value) {
                    $view->$key = $value;
                }
            }

            /*
            $view->dojo()->setDjConfigOption('parseOnLoad', true)
                     ->setLocalPath('/js/dojo/dojo.js')
                     ->registerModulePath('../spindle', 'spindle')
                     ->addStylesheetModule('spindle.themes.spindle')
                     ->requireModule('spindle.main')
                     ->disable();
            */

            $view->env = $this->getBootstrap()->getEnvironment();

            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                'ViewRenderer'
            );

            //google analytics
            if ($view->env == 'production') {
                $gaCcript = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");'
                          . 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));'
                          . 'try {'
                          . '   var pageTracker = _gat._getTracker("UA-8587498-1");'
                          . '   pageTracker._trackPageview();'
                          . '} catch(err) {}';
                $view->inlineScript()->appendScript($gaCcript);
            }

            $viewRenderer->setView($view);
            $this->_view = $view;
        }        
        return $this->_view;
    }
}